json error: Use of overloaded operator [] is ambiguous error resolution

  • 2020-06-15 09:56:45
  • OfStack

Recently, when using json arrays, it was a problem to use 0 as the subscript (Use of overloaded operator [] is ambiguous), but it was ok to use 1 as the subscript, and I was drunk. Searched everywhere on the net 1 times, the discovery also has the netizen to encounter.


// Note:
//int x = a[0].GetInt();     // Error: operator[ is ambiguous, as 0 also mean a null pointer of const char* type.
int y = a[SizeType(0)].GetInt(); // Cast to SizeType will work.
int z = a[0u].GetInt();     // This works too.

0u = 
SizeType(0)

Json::Value as an array, error reading position 0: Use of overloaded operator [] is ambiguous


Use of overloaded operator [] is ambiguous (with operand types 'const Json:Value' and 'int' )
  void Parse(constJson::Value &jsonObject) {
   rmb = jsonObject[0].asDouble();
  }

The Json:Value[] operator supports non-negative integers for input, i.e. UInt or unsigned int.

With 0 as the index value or null pointer input, the type check fails.

Modify as follows:


rmb = jsonObject[0U].asDouble();

Or:


rmb = jsonObject[SizeType(0)].asDouble();

0U represents an unsigned integer, which distinguishes between a value of 0 and a null pointer when its array operator is overloaded.

conclusion


Related articles: